import 'package:core/core.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SmartRefresh extends StatelessWidget {
  final RefreshController controller;
  final Widget child;
  final bool showNoDataWidget;
  final String noDataMsg;
  final String? noDataImg;
  final Widget? noDataWidget;
  final bool enableRefresh;
  final bool enableLoad;
  final Function()? onRefresh;
  final Function()? onLoad;
  final bool lightText;

  const SmartRefresh({
    super.key,
    required this.controller,
    required this.child,
    this.showNoDataWidget = true,
    this.noDataMsg = '暂无数据',
    this.noDataImg,
    this.noDataWidget,
    this.enableRefresh = true,
    this.enableLoad = true,
    this.onRefresh,
    this.onLoad,
    this.lightText = false,
  });

  @override
  Widget build(BuildContext context) {
    Color color = lightText ? Colors.white70 : Colors.grey;
    Widget sr = SmartRefresher(
      controller: controller,
      header: ClassicHeader(
        textStyle: TextStyle(color: color),
        failedText: '加载失败',
        failedIcon: Icon(Icons.error, color: color),
        completeText: '加载完成',
        completeIcon: Icon(Icons.done, color: color),
        idleText: '下拉刷新',
        idleIcon: Icon(Icons.arrow_downward, color: color),
        releaseText: '释放以刷新',
        releaseIcon: Icon(Icons.refresh, color: color),
        refreshingText: '下拉刷新',
        refreshingIcon: SizedBox(
          width: 25.px,
          height: 25.px,
          child: const CircularProgressIndicator(color: Colors.blueAccent),
        ),
      ),
      footer: ClassicFooter(
        textStyle: TextStyle(color: color),
        failedText: '加载失败',
        failedIcon: Icon(Icons.error, color: color),
        idleText: '上拉加载更多',
        idleIcon: Icon(Icons.arrow_upward, color: color),
        canLoadingText: '释放以加载更多',
        canLoadingIcon: Icon(Icons.auto_awesome, color: color),
        loadingText: '加载中...',
        loadingIcon: SizedBox(
          width: 25.px,
          height: 25.px,
          child: const CircularProgressIndicator(),
        ),
        noDataText: '没有更多数据了',
      ),
      onRefresh: onRefresh,
      onLoading: onLoad,
      enablePullDown: enableRefresh,
      enablePullUp: enableLoad,
      child: showNoDataWidget ? (noDataWidget ?? Center(child: Text(noDataMsg))) : child,
    );
    return sr;
  }
}
